package com.pixiv.controller;

import cn.hutool.json.JSONObject;
import com.pixiv.Entity.TitlePicture;
import com.pixiv.Entity.User;
import com.pixiv.service.TitleService;
import com.pixiv.service.UserService;
import com.pixiv.tools.SendPack;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/title")
public class TitleController {

    @Autowired
    private TitleService titleService;
    @Autowired
    private UserService userService;

    /**
     * 获得对应id帖子的第一个图片
     */
    @ResponseBody
    @RequestMapping("getTitleFristP")
    public void getTitleFristP(HttpServletRequest req, HttpServletResponse resp){
        String uuid = req.getParameter("id");

        String path = "\\title\\auditcontext\\" + titleService.getTitlePicturePathByUUIDLocation(uuid,1);
        if (path != null)
            User.outPutPictureStream(resp,path,null,null);

    }

    /**
     * 获取的特定的数量图片帖子
     * @param req
     * @param resp
     */

    @RequestMapping("getTitleP")
    public void getTitleP(HttpServletRequest req, HttpServletResponse resp)
    {
        String uuid = req.getParameter("uuid");
        int location = Integer.parseInt(req.getParameter("location"));

        String path = "\\title\\auditcontext\\" + titleService.getTitlePicturePathByUUIDLocation(uuid,location);
        if (path != null)
            User.outPutPictureStream(resp,path,null,null);
    }

    @ResponseBody
    @RequestMapping("deleteTitle")
    public String deleteTitle(HttpServletRequest req){
        HttpSession hs = req.getSession();
        User u = (User) hs.getAttribute("GLOBAL_USER_OBJECT");
        String uuid = (String) u.getVar().get_vector("GLOBAL_Title_UUID");

        int id = titleService.getTitle(uuid).getUserid();
        User wirteUser = userService.getUser(id);
        if (u.getUserID() == wirteUser.getUserID()) {
            //System.out.println("deleteEvent:uuid:" + uuid);
            titleService.deleteTitle(uuid);
            titleService.deletePicture(uuid);
            return SendPack.getFlag(1);
        }else
        {
            return SendPack.getFlag(2);
        }
    }

    @ResponseBody
    @RequestMapping("getTitleLst")
    public String getTitleLst(HttpServletRequest req){
        TitlePicture tp = new TitlePicture();
        tp.setTitleUUID(req.getParameter("titleUUID"));
        tp.setTitleCaption(req.getParameter("caption"));
        tp.setTitleLabel(req.getParameter("label"));


        List<TitlePicture> al = titleService.getTitles(tp);
        JSONObject jsonObject = new JSONObject();
        jsonObject.accumulate("count",10);
        jsonObject.accumulate("code",0);
        jsonObject.accumulate("msg","null");
        jsonObject.accumulate("data",al);
        //jsonObject.accumulate("data",)
        //System.out.println(jsonObject.toString());
        return jsonObject.toString();
    }



}
